home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7605 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.9 KB

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Using char array in functions
  5. Date: 25 Feb 1996 16:31:16 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4gqv0kINNd2k@keats.ugrad.cs.ubc.ca>
  8. References: <20FEB199609155775@sundog.caltech.edu> <4gqk5m$jno@aphex.direct.ca>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <4gqk5m$jno@aphex.direct.ca>,
  12. Ed Toivanen <etoivane@direct.ca> wrote:
  13.  >In article <20FEB199609155775@sundog.caltech.edu>, 
  14.  >taylor@sundog.caltech.edu395-3807,MC264-33CALTECHPASADENACA91125 says...
  15.  >>
  16.  >>I've searched the faq's and postings, but still am looking for a clear 
  17.  >>explanation on how to use arrays of char strings in functions, especially when 
  18.  >>I want to have the function modify the array.  I find I can't just pass the 
  19.  >>name of the string as a pointer as I do for single strings.
  20.  > ^^^^
  21.  >
  22.  >As I just learned from my midterm(the hard way, in other words!) you have to 
  23.  >pass the address of the name of the array if you expext to modify more than a 
  24.  >copy of the thing.  
  25.  
  26. If that is what you learned from the midterm, you will fail the question again
  27. if it is presented on the final---unless your instructor actually believes that
  28. arrays can be modified if you pass their address, in which case stick to the
  29. wrong answer to get the marks.
  30.  
  31.     Sucking up + marks = grad school.
  32.  
  33. However, in comp.lang.c and everywhere else, an array is not a pointer, and
  34. its location cannot be modified. When you pass an array to a function, it
  35. collapses into a pointer to the first element. This pointer exists only as
  36. a parameter---there is no variable that holds this pointer which you can
  37. modify.
  38.  
  39. This is akin to not being able to modify assembly language labels at run time.
  40. In assembly language, if you say:
  41.  
  42. .data
  43. STRING:        .ascii "My string\0"
  44.  
  45. you cannot change the value of "STRING:", because it is an address label, not a
  46. variable. (A C array is *sort of* *like* a label, but the analogy breaks down
  47. in all kinds of ways, so don't take it too literally. For example, a C array
  48. can be a local variable, but in assembly you don't declare labels that
  49. reference your procedure stack frame, just static storage.)
  50.  
  51. If you pass the address of the name of the array, as in:
  52.  
  53.     char arr[5] = "abcd";
  54.  
  55.     ...
  56.     foo(&arr);
  57.  
  58. it is basically the same pointer as if you passed foo(arr), but the type is
  59. different.
  60.  
  61. The expression  arr  is the address of arr[0], same as &arr[0] and has the
  62. type, in cast notation, (char *).
  63.  
  64. The expression  &arr  is the address of the whole array, in this case a five
  65. element array, and has the type, in cast notation (char *[5]).
  66.  
  67. You cannot change arr. Yes, this is inconsistent from the way structures are
  68. handled. The name of a structure can be assigned to, whereas the name of an
  69. array cannot be assigned to. A structure _requires_ an & operator to pass by
  70. reference else it is passed by value, whereas an array is automatically passed
  71. by reference without &, and if you use the & you get a different type. The
  72. naive ``obvious'' rule (that is, ``& passes by reference, lack of it passes by
  73. value'') seems intuitive and consistent, but unfortunately does not reflect
  74. the reality of the C language which treats functions and arrays specially.
  75.  
  76. If your instructor, in discussing the midterm results, has taught you
  77. otherwise, just send him or here to this newsgroup. 
  78.  
  79. Another array feature to watch out for is in parameter declarations. Only in
  80. function parmeter declarations, if you write "type x[]", the parameter will
  81. actually be converted to "pointer to x". Thus writing "char **argv" is the
  82. same as writing "char *argv[]". Both are pointers to pointers to char. The
  83. latter is just a more clear notation at times. Again, this special conversion
  84. reinforces the idea that arrays just collapse into pointers to the first
  85. element.
  86.  
  87. See? The language is neater than you probably thought it was...
  88.  
  89.  
  90. -- 
  91.  
  92.